<?php

namespace App\Admin\Controllers;

use App\crm_diaocha;
use App\crm_route;
use App\crm_deal;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class Crm_diaochaController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('回访调查');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(crm_diaocha::class, function (Grid $grid) {
            if(Admin::user()->isAdministrator()){
                
            }else{
                $grid->model()->where('yuangong', Admin::user()->id)->orderBy('id', 'desc');
            }
            $grid->id('序号')->sortable();
            $grid->customer_id('客户姓名');
            $grid->date('下次旅游意向日期');
            $grid->type('下次旅游意向类型');
            $grid->didian('下次旅游意向地点');
            $grid->lurudate('录入时间');
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $list=crm_route::where("parent_id",0)->pluck("title","title");
                $list2=crm_route::where("parent_id","!=",0)->pluck("title","title");
                // 在这里添加字段过滤器
                $filter->like('customer_id', '客户姓名');
                $filter->between('date', '旅游日期')->datetime();
                $filter->equal('type','旅游类型')->select($list);
                $filter->equal('didian','旅游地点')->select($list2);
            });
            $grid->disableCreation();
            $grid->disableExport();
            $grid->disableRowSelector();
             $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $order=crm_deal::find($actions->row->deal_id)->deal_orderid;
                $actions->append('<a href="'.admin_url('crm_order').'/'.$order.'/edit'.'" class="btn btn-sm btn-primary">查看成交单</a>');
            });
            $script_addones=<<<EOT
                $(".type").change(function(){
                    var id=$(".type option:selected").val();
                    $.ajax({
                        url: "/admin/gdindex/ajax12",
                        type: "get",
                        dataType: "json",
                        data: {'v': id},
                        success:function(data){
                            var hh="<option value>请-选-择<\/option>";
                            for(var i=0;i<data.length;i++){
                                hh +="<option value="
                                hh +=data[i].id
                                hh +=" >"
                                hh +=data[i].title
                                hh +="<\/option>";
                               
                            }
                            $(".didian").html(hh);
                        }
                    }); 
                })

EOT;
            Admin::script($script_addones);
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(crm_diaocha::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
    public function ajax()
    {
        $id = $_GET['v'];
        $list=crm_route::where("parent_id",crm_route::where("title",$id)->first()->id)->get();
        echo json_encode($list);
    }
}
